package com.xnf.ssmweb.controller;

import com.xnf.ssmweb.domain.Permission;
import com.xnf.ssmweb.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chendx on 2021/3/30.
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/toPermission")
    @PreAuthorize("hasRole('ROLE_ROOT')")
    public ModelAndView toPermission(){
        ModelAndView mav = new ModelAndView();
        List<Permission> permissions = permissionService.queryPermissions();
        mav.addObject("permissions",permissions);
        mav.setViewName("permission-list");
        return mav;
    }

    @RequestMapping("/toPermissionById")
    @PreAuthorize("authentication.principal.username=='root'")
    public ModelAndView toPermissionById(Long id){
        ModelAndView mav = new ModelAndView();
        Permission permission = permissionService.queryPermissionById(id);
        mav.addObject("permission",permission);
        mav.setViewName("permission-show");
        return mav;
    }


    @RequestMapping("/savePermission")
    public ModelAndView savePermission(Permission permission){
        ModelAndView mav = new ModelAndView();
        permissionService.savePermission(permission);
        mav.setViewName("redirect:/permission/toPermission");
        return mav;
    }
}
